using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TanashCMS.Business;
using TanashCMS.Business.Task;
using NHibernate;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Text;

public partial class manager_inventory_Default : CMSManagementPageBase
{
    private FormViewMode mFormMode { get { return ViewState["productFormMode"] != null ? (FormViewMode)ViewState["productFormMode"] : FormViewMode.Insert; } set { ViewState["productFormMode"] = value; } }
    protected int _categoryId { get { return Request.QueryString["cid"] != null ? int.Parse(Request.QueryString["cid"]) : 0; } }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public override void DataBind()
    {
        StringBuilder sbBreadCrumb = new StringBuilder();
        Category c = Category.Get(_categoryId);
        if (c != null)
        {
            sbBreadCrumb.Append(c.Title);
            while (c.ParentID != 0)
            {
                c = Category.Get(c.ParentID);
                if (c == null) break;
                sbBreadCrumb.Insert(0, c.Title + "&nbsp;/&nbsp;");
            }
        }
        litCategoryHeading.Text = sbBreadCrumb.ToString();

        //gvProducts.DataSource = Product.List(mWebsite.ID, "Title", 0, 0);
        rptInventory.DataSource = new InventoryTask(mWebsite.ID).GetProducts(_categoryId);
        base.DataBind();
    }

    private List<int> getSelectedProductIDs()
    {
        List<int> productIDs = new List<int>();

        foreach (RepeaterItem item in rptInventory.Items)
        {
            CheckBox cbProduct = item.FindControl("chkSelectProduct") as CheckBox;
            HiddenField fieldProductId = item.FindControl("productId") as HiddenField;
            if (cbProduct != null && cbProduct.Checked && fieldProductId != null)
                productIDs.Add(int.Parse(fieldProductId.Value));
        }

        return productIDs;
    }

    protected override void OnInit(EventArgs e)
    {
        if (!IsPostBack)
            DataBind();

        if (mWebsite == null)
            return;

        List<ProductOptionType> optionTypes = ProductOptionType.List(mWebsite.ID);

        base.OnInit(e);
    }

    private void DeleteSelectedProducts()
    {
        ISession session = Configuration.GetCurrentSession();
        ITransaction tx = session.BeginTransaction();

        try
        {
            foreach (int productID in getSelectedProductIDs())
            {
                Product product = session.Get<Product>(productID);
                session.Delete(product);
            }
            mWebsite.NeedsPublish = true;
            session.Save(mWebsite);
            session.Flush();
            tx.Commit();
            ShowSystemFeedback("The products have been deleted");
        }
        catch (Exception ex)
        {
            tx.Rollback();
            session.Disconnect();
            string msg = ex.Message;
            while (ex.InnerException != null)
            {
                ex = ex.InnerException;
                msg += "<br/>" + ex.Message;
            }
            ShowSystemFeedback("A problem occurred", ex.Message, System.Drawing.Color.Red);
        }

        DataBind();
    }

    protected void ddlAction_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (ddlAction.SelectedValue)
        {
            case "delete":
            DeleteSelectedProducts(); break;
        }

        ddlAction.SelectedIndex = 0;
    }

    protected void rptInventory_OnItemCommand(object sender, RepeaterCommandEventArgs args)
    {
        if (args.CommandName == "MoveUp")
            new InventoryTask(mWebsite.ID).ProductOrderUp(int.Parse(args.CommandArgument.ToString()), User.Identity.Name);
        else if (args.CommandName == "MoveDown")
            new InventoryTask(mWebsite.ID).ProductOrderDown(int.Parse(args.CommandArgument.ToString()), User.Identity.Name);
        DataBind();
    }

}
